//
//  NewsImageListViewController.m
//  Project1
//
//  Created by 黄珂耀 on 15/7/22.
//  Copyright (c) 2015年 黄珂耀. All rights reserved.
//

#import "NewsImageListViewController.h"
#import "common.h"
#import "NewsImageCell.h"
#import "jsonAnalysis.h"
#import "PhotoViewController.h"
#import "BaseNavigationController.h"

@interface NewsImageListViewController ()

@end

@implementation NewsImageListViewController

#pragma mark - DefaultOperation

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"图片新闻";
    
    [self _loadData];
    
    [self _createSubviews];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LoadData

- (void)_loadData
{
    _newsImageArray = [[NSMutableArray alloc] init];
    
    NSArray *rootArray = [jsonAnalysis initWithJsonFileName:@"image_list.json"];
    
    for (NSDictionary *dic in rootArray)
    {
        NewsImageModal *modal = [[NewsImageModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_newsImageArray addObject:modal];
    }
}


#pragma mark - CreateSubviews

- (void)_createSubviews
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((KWidth - 50) / 4, (KWidth - 50) / 4);
    
    _newsImageListView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    
    _newsImageListView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    _newsImageListView.delegate = self;
    _newsImageListView.dataSource = self;
    
    [_newsImageListView registerClass:[NewsImageCell class] forCellWithReuseIdentifier:@"imageCell"];
    
    [self.view addSubview:_newsImageListView];
    
}

#pragma mark - CollectonViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_newsImageArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NewsImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
    
    cell.modal = _newsImageArray[indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoViewController *photoVc = [[PhotoViewController alloc] init];
    photoVc.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    NSInteger index = indexPath.row;
    
    photoVc.currentIndex = index;
    
    NSMutableArray *imageURLArray = [[NSMutableArray alloc] init];
    
    for (NewsImageModal *modal in _newsImageArray)
    {
        
        NSString *imageURL = modal.image;
        [imageURLArray addObject:imageURL];
    }

    photoVc.photoURLArray = imageURLArray;
    
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:photoVc];
    
    [self presentViewController:nav animated:YES completion:nil];
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
